package com.czl.elm.service.impl;

import com.czl.elm.dao.SearchMapper;
import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private SearchMapper searchMapper;
    @Value("${elm.SEARCHMAX}")
    private Integer SEARCHMAX;

    /**
     * 查询搜索商店的历史记录
     * @return
     */

    @Override
    public List<String> searchShopHistory() {
        String phoneName=((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        List<String> res=searchMapper.searchShopHistory(phoneName);
        return res;
    }


    /**
     * 插入搜索商店的历史记录
     * @param req
     */
    @Override

    public void insertShopHistory( String req) {
        String phoneName=((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        List<String> list=searchMapper.searchShopHistory(phoneName);

        boolean flag=true;
        for(String s:list){
            if(s.equals(req)){
                flag=false;
                list.remove(req);
                list.add(0,req);
                break;
            }
        }
       if(flag){
           if(list.size()==SEARCHMAX){
               list.remove(list.size()-1);
           }
           list.add(0,req);
       }
        searchMapper.insertShopHistory(phoneName,list);
    }

    /**
     * 删除搜索商店的历史记录
     */
    @Override
    public void delShopHistory() {
        String phoneName=((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        searchMapper.delShopHistory(phoneName);
    }
}
